﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using DataAccess.Classes;

public partial class Controls_ProductsList : System.Web.UI.UserControl
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack)
        {
            PopulateControls();
        }
    } 

    private void PopulateControls() 
    {
        int howManyPages = 0; 
        string categoryId = Request.QueryString["MaThuongHIeu"] ?? "";
        string page = Request.QueryString["Page"] ?? "1";
        string searchString = Request.QueryString["Search"] ?? "";
        int giaDuoi, giaTren;
        try
        {
            giaDuoi = int.Parse(Request.QueryString["Price"] ?? "-1");
        }
        catch
        {
            giaDuoi = -1;
        }
        try
        {
            giaTren = int.Parse(Request.QueryString["to"] ?? "12345");
        }
        catch
        {
            giaTren = 12345;
        }       
        string firstPageUrl = ""; 
        string pagerFormat = "";
        if (searchString != "" || giaDuoi != -1 || giaTren != 12345)
        {
            dlProdList.DataSource = HangHoa.Search(searchString, giaDuoi, giaTren, page, out howManyPages);
            dlProdList.DataBind();
            firstPageUrl = Link.ToSearch(searchString, giaDuoi, giaTren, "1");
            pagerFormat = Link.ToSearch(searchString, giaDuoi, giaTren, "{0}");
        }
        else if (categoryId != "")
        {
            dlProdList.DataSource = HangHoa.InCategory(categoryId, page, out howManyPages);
            dlProdList.DataBind();
            firstPageUrl = Link.ToCategory(categoryId);
            pagerFormat = Link.ToCategory(categoryId, "{0}");
        }
        else
        {
            dlProdList.DataSource = HangHoa.OnPromo(page, out howManyPages);
            dlProdList.DataBind();
            firstPageUrl = Link.ToCatalog("1");
            pagerFormat = Link.ToCatalog("{0}");
        } 
        pagerTop.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false); 
        pagerBottom.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, true);
    } 
}